package net.expectx.gw.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.expectx.gw.bean.SystemType;
import net.expectx.gw.service.SystemTypeService;
import net.expectx.gw.util.AdminConstants;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * controller
 * @author lijian
 * @date 2019/1/18
 */
@Controller
@RequestMapping("/manager/system/systemType")
@Api(value = "控制器", description = "管理")
public class SystemTypeController extends BaseController {

    private static final Logger LOGGER = LoggerFactory.getLogger(SystemTypeController.class);

    @Autowired
    private SystemTypeService systemTypeService;

    @ApiOperation(value = "字典主页")
    @RequestMapping(value = "/index/{id}", method = RequestMethod.GET)
    public String index(@PathVariable("id") String id, HttpServletRequest request) {

        request.getSession().setAttribute("id",id);
        return "type/index";
    }

    @ApiOperation(value = "新增字典")
    @RequestMapping(value = "/create", method = RequestMethod.GET)
    public String create() {
        return "type/modify";
    }

    @ApiOperation(value = "修改字典")
    @RequestMapping(value = "/update/{id}", method = RequestMethod.GET)
    public String update(@PathVariable("id") Long id, ModelMap modelMap) {
        SystemType systemType = systemTypeService.selectByPrimaryKey(id);
        modelMap.put("systemType", systemType);
        return "type/modify";
    }

    @RequestMapping(value = "/list/{id}", method = RequestMethod.GET)
    @ResponseBody
    public Object list(
            @RequestParam(required = false, defaultValue = "1", value = "page") int page,
            @RequestParam(required = false, defaultValue = "10", value = "limit") int limit,
            HttpServletRequest request,@PathVariable("id")String id) {

        String strWhere = "is_deleted=0";
        String strOrder = null;
        if(id!=null){
            strWhere+=" and type_group_id in("+id+")";
        }
        String typeName = request.getParameter("typeName");
        if (StringUtils.isNotBlank(typeName)) {
            strWhere += "and type_name like('%" + typeName + "%')";
        }
        List<SystemType> systemTypes = systemTypeService.findByPage(page, limit, strWhere, strOrder);
        long total = systemTypeService.count(strWhere);
        Map<String, Object> result = new HashMap<>(16);
        result.put("code", AdminConstants.LAYUI_DATA_OK);
        result.put("count", total);
        result.put("msg", "");
        result.put("data", systemTypes);
        return result;
    }

    @ApiOperation(value = "新增字典")
    @RequestMapping(value = "/create/{id}", method = RequestMethod.POST)
    @ResponseBody
    public Object create(@PathVariable("id")long id,SystemType systemType) {
        systemType.setGmtCreate(new Date());
        systemType.setGmtModified(new Date());
        systemType.setIsDeleted(AdminConstants.NO);
        systemType.setTypeGroupId(id);
        return systemTypeService.insertSelective(systemType);
    }

    @ApiOperation(value = "修改字典")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public Object update( SystemType systemType) {
        systemType.setGmtModified(new Date());


        return systemTypeService.updateByPrimaryKeySelective(systemType);
    }

    @ApiOperation(value = "删除字典")
    @RequestMapping(value = "/delete/{ids}", method = RequestMethod.GET)
    @ResponseBody
    public Object delete(@PathVariable("ids") String ids) {
        Map<String, Object> map = new HashMap<>(2);
        int count = systemTypeService.logicDeletion(AdminConstants.YES, "id in(" + ids + ")");
        if (count > 0) {
            count = systemTypeService.logicDeletion(AdminConstants.YES, "id in(" + ids + ")");
            map.put("flag", count > 0 ? true : false);
        } else {
            map.put("flag", count > 0 ? true : false);

        }

        return map;
    }

}
